package untils

import (
	"bytes"
	"context"
	"encoding/json"
	"github.com/elastic/go-elasticsearch/v7/esapi"
	"log"
	"service/basic/config"
	"strconv"
)

type Es struct {
	Index string
	Id    int64
	Body  interface{}
}

func (e *Es) SyncData() {
	data, err := json.Marshal(e.Body)
	if err != nil {
		log.Fatalf("Error marshaling document: %s", err)
	}

	// Set up the request object.
	req := esapi.IndexRequest{
		Index:      e.Index,
		DocumentID: strconv.FormatInt(e.Id, 10),
		Body:       bytes.NewReader(data),
		Refresh:    "true",
	}

	// Perform the request with the client.
	res, err := req.Do(context.Background(), config.ES)
	if err != nil {
		log.Fatalf("Error getting response: %s", err)
	}
	defer res.Body.Close()
}
